package com.yang.service.adminimpl;

import com.yang.bean.*;
import com.yang.bean.yvo.SelectUserPermsBean;
import com.yang.mapper.MarketPermissionMapper;
import com.yang.mapper.MarketRoleMapper;
import com.yang.service.admin.AdminAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.9 22:05
 * @description:
 */
@Service
public class AdminAuthServiceImpl implements AdminAuthService {

    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public SelectUserPermsBean selectUserPerms(MarketAdmin marketAdmin) {

        SelectUserPermsBean selectUserPermsBean = new SelectUserPermsBean();
        selectUserPermsBean.setAvatar(marketAdmin.getAvatar());
        selectUserPermsBean.setName(marketAdmin.getUsername());
        //TODO 返回用户的权限信息
        //角色
        String roleIds = marketAdmin.getRoleIds();
        //"[1,2,3]
        int index = roleIds.indexOf("[");
        int index1 = roleIds.indexOf("]");
        String substring = roleIds.substring(index+1, index1);
        //1,2,3
        String[] split = substring.split(",");

        ArrayList<Integer> list = new ArrayList<>();
        for (String s : split) {
            int i = Integer.parseInt(s);
            list.add(i);
        }
        if (list.get(0) == 1) {
            ArrayList<String> strings = new ArrayList<>();
            strings.add("*");
            selectUserPermsBean.setPerms(strings);
        }else {
            //根据角色的id查询perms
            MarketPermissionExample example = new MarketPermissionExample();
            MarketPermissionExample.Criteria criteria = example.createCriteria();
            criteria.andRoleIdIn(list);
            List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
            //TODO 获取perms
            //TODO perms的格式 POST /admin/category/delete 怎么确定什么是post 什么是get

            ArrayList<String> perms = new ArrayList<>();
            if (marketPermissions.size()>0){
                for (MarketPermission marketPermission : marketPermissions) {
                    String permission = marketPermission.getPermission();
                    String url = permission.replaceAll(":", "/");
                    String realUrl = "/" +url;
                    //TODO list read detail listuser mall express order wx user
                    if (realUrl.contains("list") || realUrl.contains("read") || realUrl.contains("listRecord")
                            || realUrl.contains("detail") || realUrl.contains("listuser")
                            || realUrl.contains("mall") || realUrl.contains("express")
                            || realUrl.contains("order") || realUrl.contains("user")
                            || realUrl.contains("wx")) {
                        String permsZ = "GET " + realUrl;
                        perms.add(permsZ);
                    } else if ("*".equals(permission)) {
                        perms.add(permission);
                    } else {
                        String permsZ = "POST " + realUrl;
                        perms.add(permsZ);
                    }
                }
            }
            selectUserPermsBean.setPerms(perms);
        }

        //TODO 就是用户的roles 根据roles——id查询role的name
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria1 = marketRoleExample.createCriteria();
        criteria1.andIdIn(list);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        ArrayList<String> rolename = new ArrayList<>();
        if (marketRoles.size() > 0) {
            for (MarketRole marketRole : marketRoles) {
                rolename.add(marketRole.getName());
            }
        }
        selectUserPermsBean.setRoles(rolename);
        return selectUserPermsBean;
    }
}
